import Config from './config.js'
import Util from './utils.js'
import store from '@/store/index.js'
import aes_utils from '@/js_sdk/aesutils/aes_utils.js'
import preutils from './preutils.js'
import check from './check.js'
// #ifdef H5
const app_type = Util.isWeiXin() ? 'wechat' : 'h5';
const app_type_name = Util.isWeiXin() ? '微信公众号' : 'H5';
// #endif

// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

// #ifdef MP-ALIPAY
const app_type = 'aliapp';
const app_type_name = '支付宝小程序';
// #endif

// #ifdef MP-BAIDU
const app_type = 'baiduapp';
const app_type_name = '百度小程序';
// #endif

// #ifdef MP-TOUTIAO
const app_type = 'MP-TOUTIAO';
const app_type_name = '头条小程序';
// #endif

// #ifdef MP-QQ
const app_type = 'MP-QQ';
const app_type_name = 'QQ小程序';
// #endif

// #ifdef APP-PLUS
// const app_type = 'app';
// const app_type_name = 'APP';
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

export default {
	 
	sendRequest(params) {
		var url = ''
		var method = ''
		url = Config.baseUrl + params.api.url; // 请求路径
		method = params.api.method;
		if(method == undefined ){
			method = 'GET';
		}
		try {
		    var accessToken = uni.getStorageSync(preutils.accessToken);
			var loginToken = uni.getStorageSync(preutils.loginToken);
			var userType = uni.getStorageSync(preutils.userType);
		} catch (e) {
			
		} 
		
		if(!check.isNull(loginToken)){
			params.data.access_token = loginToken;
		}
		
		let params_decrypt = {
	     ...params.data
	    }
		if (params.api.isAes) {
		    for (let key in params.data) { // 在这里判断传过来的参数值为null，就删除这个属性
		    	params.data[key] = aes_utils.AesEncrypt(params.data[key])
	        }  
		}
		var clientKeyValue = ''
		if (userType == 1) {
			clientKeyValue = preutils.shopKeyValue
	    }else{
			clientKeyValue = preutils.driverKeyValue
		}	
		var headers = {
		 	'Operation-System' : 'IOS',
		 	'Client-Key' : clientKeyValue,
		 	'Client-Version' : '1.0.0',
		 	'Authorization' : 'Bearer '+ accessToken,
		 	'access_token' : loginToken,
		 	// 'content-type': method == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded',
			"content-type": "multipart/form-data",
		 }
		params.header = headers
		 
		var data = {
			app_type,
			app_type_name
		};
		// 参数
		if (params.data != undefined) Object.assign(data, params.data);
		
		if(params.isFiles){
			uni.uploadFile({
				url: url, //接口
				filePath:params.imageData,
				// filePath: params.imageData.tempFilePaths[0],
				// formData: {
				//   'image': params.imageBase,
				// },
				name:params.api.fileType,
				header: params.header || {
				},
				// file:params.imageBase,
				// file: params.imageData.tempFiles[0],
				// dataType:  'image',
				success: res => {
					console.log(res)
					if (params.api.isAes) {
						if(res.statusCode == 200){
						const jsondata = JSON.parse(res.data)
						  var decryptdata = JSON.parse(aes_utils.AesDecrypt(jsondata.data));
						   jsondata.data = decryptdata
						   res.data= jsondata;
						}
					} 
					this.printSuccessLog(url,method,params_decrypt,params,'success',res)
					typeof params.success == 'function' && params.success(res.data);
				},
				fail: res => {
					console.log(res);
					this.printErrLog(url,method,params_decrypt,params,'fail',res)
					typeof params.fail == 'function' && params.fail(res);
				}
			});
		}else{
			uni.uploadFile({
				url: url, //接口
				filePath: params.imageData.tempFilePaths[0],
				// formData: {
				//   'image': params.imageBase,
				// },
				name:params.api.fileType,
				header: params.header || {
				},
				// file:params.imageBase,
				// file: params.imageData.tempFiles[0],
				// dataType:  'image',
				success: res => {
					if (params.api.isAes) {
						if(res.statusCode == 200){
						const jsondata = JSON.parse(res.data)
						  var decryptdata = JSON.parse(aes_utils.AesDecrypt(jsondata.data));
						   jsondata.data = decryptdata
						   res.data= jsondata;
						}
					} 
					this.printSuccessLog(url,method,params_decrypt,params,'success',res)
					typeof params.success == 'function' && params.success(res.data);
				},
				fail: res => {
					console.log(res);
					this.printErrLog(url,method,params_decrypt,params,'fail',res)
					typeof params.fail == 'function' && params.fail(res);
				}
			});
		}
	},
	printSuccessLog(url,method,data,params,title,res){
		var urls = ''
		urls = params.api.url
		var myObj1 = {'url:':url,'data_decrypt:':data,'res':res.data};
		 	// console.log("Success-------->>>\n: " + JSON.stringify(myObj1));
	},
	printCompleteLog(url,method,data,params,title,res){
		var urls = ''
		if(params.api != undefined){
			urls = params.api.url
		}else{
			urls = params.url
		}
		var myObj1 = {'url:':url,'method:':method,'params:':params,'data_decrypt:':data,'res':res.data };
	},
	printErrLog(url,method,data,params,title,res){
		var urls = ''
		if(params.api != undefined){
			urls = params.api.url
		}else{
			urls = params.url
		}
		var myObj1 = {'url:':url,'method:':method,'params:':params,'data_decrypt:':data,'res':res.data };
		console.log("Err-------->>>\n: " + JSON.stringify(myObj1));
	}, 
	
}
